var o = require("../UISoundManager.js");
cc.Class({
    extends: cc.Component,
    properties: {
        itemPre: cc.Prefab,
        itemContent: cc.Node,
        content: cc.Node,
        existContentArr: [cc.Node],
        newSpf: cc.SpriteFrame,
        hotSpf: cc.SpriteFrame
    },
    onLoad: function () {
        Global.notificationCenter.on(Global.eventName.MSH_ACTIVITY_CLICK_ITEM, this.operateItem.bind(this), this);
        var e = Global.configCdn.getActivityConf() || Global.configCdn.getDefaultConfig("activity");
        console.log("activityHelper config:", e), this.conf = e, this.acticityComp = this.getComponent("ActivityPanel"),
            this.spfObj = {};
    },
    start: function () {
        this.conf ? this.initData() : console.log("加载失败");
    },
    onDestroy: function () {
        Global.notificationCenter.offAll(this);
    },
    initData: function () {
        for (var e = 0; e < this.conf.length; e++) {
            var t = function (e) {
                var t = cc.instantiate(this.itemPre);
                this.itemContent && this.itemContent.addChild(t), this.setItemData(t, this.conf[e]);
            }.bind(this);
            if (this.conf[e].attention) switch (this.conf[e].attention.condition) {
                case "isFirstRecharge":
                    Global.GetIsFirstRecharge() || t(e);
            } else t(e);
            this.setContentData(this.conf[e], e);
        }
        this.setDefaultData();
    },
    setDefaultData: function () {
        var e = this.conf[0], t = !!e.contentUrl, n = {
            index: 0,
            isShowRich: !!e.richStr,
            isShowSp: t,
            existContentName: e.existContentName,
            durationStr: e.durationStr || ""
        };
        if (this.currentItemIndex = 0, t) {
            var o = this;
            o.spfObj[0] ? Global.notificationCenter.emit(Global.eventName.MSH_ACTIVITY_CLICK_ITEM, n) : cc.loader.load(e.contentUrl, function (e, t) {
                if (null == e) {
                    var i = new cc.SpriteFrame(t);
                    o.spfObj[0] = i, Global.notificationCenter.emit(Global.eventName.MSH_ACTIVITY_CLICK_ITEM, n);
                }
            });
        } else Global.notificationCenter.emit(Global.eventName.MSH_ACTIVITY_CLICK_ITEM, n);
    },
    operateItem: function (e) {
        for (var t = e.index, n = e.isShowSp, o = e.isShowRich, i = e.existContentName, a = e.durationStr, r = 0; r < this.itemContent.children.length; r++) {
            this.itemContent.children[r];
            var s = !1;
            t == r && (s = !0), this.initUI(r, t, s, n, o, i, a);
        }
    },
    setItemData: function (e, t) {
        var n = t.labStr, o = Global.utils.searchComp(e, "lab", cc.Label),
            i = Global.utils.searchComp(e, "label", cc.Label);
        o.string = n, i.string = n;
        var a = !!t.contentUrl, r = !!t.richStr, s = t.existContentName, l = !!t.isNew, c = !!t.isHot;
        if (l || c) {
            var d = e.getChildByName("flag").getComponent(cc.Sprite);
            d.spriteFrame = l ? this.newSpf : this.hotSpf, d.node.active = !0;
        }
        e.on("touchend", this.onClickItem.bind(this, e, {
            isShowSp: a,
            isShowRich: r,
            existContentName: s
        }), this);
    },
    setContentData: function (e, t) {
        var n = this, o = e.contentUrl, i = e.richStr;
        if (o) {
            var a = Global.utils.searchComp(this.content, "contentSp", cc.Sprite);
            if (!this.isRegister) this.isRegister = !0, a.node.on("touchend", this.onClickPic.bind(this), this);
            cc.loader.load(e.contentUrl, function (e, o) {
                if (null == e) {
                    var i = new cc.SpriteFrame(o);
                    cc.isValid(n.spfObj) && (n.spfObj[t] = i);
                }
            });
        }
        i && "" != i && (Global.utils.searchNode(this.content, "richNode").getChildByName("richText").getComponent(cc.RichText).string = i);
    },
    initUI: function (e, t, n, o, i, a, r) {
        this.itemContent.children[e].getChildByName("selected").active = n, this.content.getChildByName("richNode").active = i,
        o && this.spfObj[e] && t == e && (this.content.getChildByName("contentSp").getComponent(cc.Sprite).spriteFrame = this.spfObj[e]),
            this.content.getChildByName("contentSp").active = o;
        for (var s = 0; s < this.existContentArr.length; s++) if (this.existContentArr[s].active = !1,
        this.existContentArr[s].name == a) {
            this.existContentArr[s].active = !0;
            var l = Global.utils.searchComp(this.existContentArr[s], "labDuration", cc.Label);
            l && r && "" != r && (l.string = r);
        }
    },
    onClickItem: function (e, t) {
        o.playEffect("select");
        var n = e.getSiblingIndex(), i = t.isShowSp, a = {
            index: n,
            isShowRich: t.isShowRich,
            isShowSp: i,
            existContentName: t.existContentName
        };
        this.currentItemIndex = n, Global.notificationCenter.emit(Global.eventName.MSH_ACTIVITY_CLICK_ITEM, a);
    },
    onClickPic: function () {
        if (this.conf) {
            var e = this.currentItemIndex;
            this.conf[e].needClickSound && o.playEffect("select");
            var t = this.conf[e].todotask, n = t.action, i = t.popupName, a = t.productId, r = t.buyType,
                s = t.component, l = t.funcName, c = t.defaultPlayMode, d = t.gameId;
            switch (n) {
                case "recharge":
                    if (a) {
                        var u = Global.configCdn.getShopIdsConf() || Global.configCdn.getDefaultConfig("shopIds");
                        this.showCommonCharge(u, a, r);
                    }
                    break;

                case "popup":
                    i && this.showPopup(i);
                    break;

                case "cloneClickEvent":
                    var h = SceneHelper.getCurScene();
                    if (s && l && h.getComponent(s) && h.getComponent(s)[l]) {
                        var f = h.getComponent(s);
                        f[l].call(f);
                    }
                    i && this.showPopup(i);
                    break;

                case "quickStart":
                    var p = {
                        playMode: c,
                        gameId: d,
                        type: c
                    };
                    Global.notificationCenter.emit(Global.eventName.MSG_DO_QUICK_START, p);
            }
        }
    },
    showCommonCharge: function (e, t, n) {
        var o = {
            prodPrice: 0,
            desc: "",
            prodId: "",
            prodName: "",
            buyType: ""
        };
        for (var i in e) if (i == t) {
            o.prodPrice = res[i].price, o.desc = res[i].desc, o.prodId = res[i].productId, o.prodName = res[i].displayName,
                o.buyType = n;
            break;
        }
        Global.SDK.recharge(o);
    },
    showPopup: function (e) {
        switch (e) {
            case "Panel_recharge":
                Global.GetIsFirstRecharge() || Global.UIPopupManager.addPopup("PopupPanel/" + e, function (e) {
                    e.getComponent("RechargePanel").bindData(null, 3);
                });
        }
    }
})